package org.jeecg.modules.engineering.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.engineering.entity.EquipCollectItem;
import org.jeecg.modules.engineering.entity.EquipEventSort;
import org.jeecg.modules.engineering.entity.EquipInfo;
import org.jeecg.modules.engineering.mapper.EquipCollectItemMapper;
import org.jeecg.modules.engineering.mapper.EquipEventSortMapper;
import org.jeecg.modules.engineering.service.IEquipEventSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 设备事件分类表
 * @Author: jeecg-boot
 * @Date:   2019-08-20
 * @Version: V1.0
 */
@Service
public class EquipEventSortServiceImpl extends ServiceImpl<EquipEventSortMapper, EquipEventSort> implements IEquipEventSortService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private EquipEventSortMapper equipEventSortMapper;

    @Override
    public List<EquipEventSort> selectAll() {
        return equipEventSortMapper.selectAll();
    }

    @Override
    public void saveWithRedis(EquipEventSort entity) {
        //存储到数据库
        boolean result = save(entity);
        if(result){
            //以ID为key，存储到redis
            setToRedis(entity);
        }
    }

    @Override
    public boolean updateWithRedis(EquipEventSort entity) {
        EquipEventSort oldEntity = getById(entity.getId());
        boolean result = updateById(entity);
        if(result){
            //先移除编辑前的redis键值
            removeFromRedis(oldEntity);
            //更新成功后，重新设置redis
            setToRedis(entity);
        }
        return result;
    }

    @Override
    public void removeWithRedis(String id) {
        EquipEventSort entity = getById(id);
        //从数据库删除
        boolean result = removeById(id);
        if(result){
            //从redis删除
            removeFromRedis(entity);
        }
    }

    private void setToRedis(EquipEventSort entity){
        if(entity == null){
            return;
        }
        redisUtil.set(CacheConstant.REDIS_TABLES+"equip_event_sort:id:"+entity.getId(), JSONObject.toJSONString(entity));
        redisUtil.set(CacheConstant.REDIS_TABLES+"equip_event_sort:otherKey:"+entity.getSortCode(),entity.getId());
    }

    @Override
    public void removeFromRedis(String id){
        removeFromRedis(findById(id));
    }

    @Override
    public void removeFromRedis(EquipEventSort entity){
        if(entity == null){
            return;
        }
        redisUtil.del(CacheConstant.REDIS_TABLES+"equip_event_sort:id:"+entity.getId()
                ,CacheConstant.REDIS_TABLES+"equip_event_sort:otherKey:"+entity.getSortCode()
                );
    }

    @Override
    public EquipEventSort getFromRedisByOtherKey(String key){
        String id = (String) redisUtil.get(CacheConstant.REDIS_TABLES+"equip_event_sort:otherKey:"+key);
        if(StringUtils.isEmpty(id)){
            return null;
        }
        return getFromRedisById(id);
    }

    @Override
    public EquipEventSort getFromRedisById(String id){
        EquipEventSort entity = JSONObject.parseObject((String) redisUtil.get(CacheConstant.REDIS_TABLES+"equip_event_sort:id:"+id),EquipEventSort.class) ;
        if(entity == null){
            //从数据库查询
            entity = findById(id);
            if(entity != null){
                //重新设置redis
                setToRedis(entity);
            }
        }
        return entity;
    }

    private EquipEventSort findById(String id) {
        return equipEventSortMapper.findById(id);
    }

    @Override
    public Result<String> refreshToRedis() {
        List<EquipEventSort> lists = list();
        if(lists != null && lists.size() > 0){
            for (EquipEventSort entity : lists){
                setToRedis(entity);
            }
        }
        return Result.ok();
    }

    @Override
    public void batchRemoveWithRedis(List<String> ids) {
        if(ids != null && ids.size() > 0){
            for (String id :ids){
                removeWithRedis(id);
            }
        }
    }

    @Override
    public IPage<EquipEventSort> selectSqlList(Page<EquipEventSort> page, String sortName, String sortCode) {
        return page.setRecords(equipEventSortMapper.selectSqlList(page,sortName,sortCode));
    }

}
